#!/bin/sh

current_path=$(cd $(dirname $0) && pwd )
echo $current_path/

# Because stopping the service first will cause the upgrade thread to also be interrupted,
# replace the file first, in kill 9 EagleEyeSMG/, and then the appm daemon will automatically pull up.
#Upgrade operation

#1.Backup conf
cp -r /userdata/tmp/app/EagleEyeSMG/conf /userdata

#2.Replace new file
cp -r ./* ../../
if [ $? -ne 0 ];then
    echo "cp -r ./* ../ ERROR"	
    echo "error $(pwd)" > ../../update.tmp
    exit 1
fi

echo 1
#3
cp -r /userdata/conf/* /userdata/tmp/app/EagleEyeSMG/conf

#4.
rm -rf /userdata/conf

#5.
content=$(cat ../../conf/EagleEyeSMG.ini)

# 删除 version_no = 的行
content=$(echo "$content" | grep -v "version_no =")

# 
content=$(echo "$content" | awk '/\[version\]/{print; print "version_no = SE01.023"; next}1')

# 将修改后的内容写回 EagleEyeSMG.ini 文件
echo "$content" > ../../conf/EagleEyeSMG.ini

#6.Create an update.tmp file, which is an indicator of the success or failure of the upgrade
echo "success" > ../../update.tmp

#6.kill -9 EagleEyeSMG 

#processArr=("EagleEyeSMG")
echo 5
#for process in ${processArr[*]}
#do
    pidArr=$(pgrep -f "EagleEyeSMG")
    echo $pidArr
    for id in $pidArr
    do
        kill -9 $id
        echo "killed $process(pid:$id)"
    done
#done


#Happy ending
echo "Upgrade completed successfully."
exit 0

